function strToJson(str) {
    var json = (new Function("return " + str))();
    return json;
}

var Schedule = {
    controlId: "",
    isMouseDown: false,
    mousedownTarget: "",
    data: new Map(),
    date: new Map(),
    init: function (controlId, data, isHour) {
        Schedule.controlId = controlId;
        Schedule.addHeader();
        if (isHour == "0") {
            return;
        }
        var control = $("#" + controlId);
        var jsonData = strToJson(data);
        var tbody = control.find("tbody");

        for (i in jsonData) {
            Schedule.addRow(i, jsonData[i].date);
            for (j in jsonData[i].data) {
                $("#" + i + "_" + jsonData[i].data[j]).addClass("selected");
                Schedule.data.set(i + "_" + jsonData[i].data[j], "1");
            }
        }
        Schedule.bindEvent();
    },
    bindEvent: function () {
        var control = $("#" + Schedule.controlId);
        control.find("td").mousedown(function (event) {
            var tb = $(this);
            Schedule.mousedownTarget = event.target.id;
            Schedule.isMouseDown = true;

        });

        control.find("td").mouseover(function (event) {
            if (!Schedule.isMouseDown) return false;

            $("#" + Schedule.mousedownTarget).addClass("selected");
            Schedule.data.set($("#" + Schedule.mousedownTarget).attr("id"), "1");

            var tb = $(this);
            tb.addClass("selected");
            Schedule.data.set(tb.attr("id"), "1");
        });


        control.find("td").mouseup(function (event) {
            Schedule.isMouseDown = false;
        });

        control.find("td").click(function () {
            var tb = $(this);
            var id = tb.attr("id");

            if (Schedule.data.get(id)) {
                $(this).removeClass("selected");
                Schedule.data.delete(id);
            } else {
                $(this).addClass("selected");
                Schedule.data.set(tb.attr("id"), "1");
            }
        });
    },
    getData: function () {
        var r = new Map();
        Schedule.data.forEach(function (value, key) {
            var d = key.split("_");
            var date = $("#" + d[0] + "_date").html();
            if (date) {

                if (r.get(date)) {
                    r.get(date).push(d[1]);
                } else {
                    r.set(date, [d[1]]);
                }
            }
        });
        return r;
    },
    addHeader: function () {
        var html = [];
        html.push("<thead><tr><th width='10%'>Date</th>");
        for (var i = 0; i < 24; i++) {
            html.push("<th>" + i + "</th>");
        }
        html.push("</tr></thead><tbody></tbody>");
        $("#" + Schedule.controlId).html(html.join(""));
    },
    addRow: function (i, date) {
        var html = [];
        html.push("<tr id='" + i + "_row'><th id='" + i + "_date'>" + date + "</th>");
        for (var j = 0; j < 24; j++) {
            html.push("<td id=\"" + i + "_" + j + "\">&nbsp;&nbsp;</td>");
        }
        html.push("</tr>");

        var tbody = $("#" + Schedule.controlId).find("tbody");
        tbody.html(tbody.html() + html.join(""));
        Schedule.date.set(date, i);
    },
    removeRow: function (i) {
        $("#" + i + "_row").remove();
        Schedule.date.delete(i + "_row");
        Schedule.data.forEach(function (value, key) {
            var d = key.split("_");
            if (i == d[0]) {
                Schedule.data.delete(key);
            }
        });
    },
    refresh: function (newDate) {
        if (!$("#" + Schedule.controlId).find("thead")) {
            Schedule.addHeader();
        }
        newDate.forEach(function (value, key) {
            if (!Schedule.date.get(key)) {
                Schedule.addRow(Schedule.date.size + "", key);
            }
        });
        Schedule.date.forEach(function (value, key) {
            if (!newDate.get(key)) {
                Schedule.removeRow(value);
            }
        });
        Schedule.bindEvent();
    },
    clean: function () {
        Schedule.data = new Map();
        Schedule.date = new Map();
        $("#" + Schedule.controlId).find("tbody").html("");
    }
};